package com.demo.chapter10optional;

import java.util.Optional;

import com.demo.domain.Car;
import com.demo.domain.Insurance;
import com.demo.domain.Person;

//使用flatMap链接多个Optional对象
public class FlatMapDemo {
	
	public static void demo(Person person){
		Optional<Person> optPerson = Optional.ofNullable(person);
		//返回保险公司的名字，每一级的内容如果不存在就会返回空Optional
		//flatMap里面的Function方法要返回Optional
		String name = optPerson
			.flatMap(Person::getCarOptional)
			.flatMap(Car::getInsuranceOptional)
			.map(Insurance::getName)
			.orElse("unknow");
	}

}
